package com.larissa.android.quiz;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.MenuProvider;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.larissa.android.quiz.fragments.QuestionFragment;
import com.larissa.android.quiz.question.QuestionRepository;

public class QuestionActivity extends AppCompatActivity {

    private NavHostFragment navHostFragment;


    private final MenuProvider menu = new MenuProvider() {

        @Override
        public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
            menuInflater.inflate(R.menu.menu_question, menu);
        }

        @Override
        public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {
            int id = menuItem.getItemId();
            if (id == R.id.menu_switch_category) {
                if (currentCategory == WelcomeActivity.GEOGRAPHY) {
                    currentCategory = WelcomeActivity.MATHEMATICS;
                } else {
                    currentCategory = WelcomeActivity.GEOGRAPHY;
                }
                updateCategoryAndScreen();
                return true;
            }
            if (id == R.id.menu_switch_screen) {
                if (currentScreen == RecyclerView.VERTICAL) {
                    currentScreen = RecyclerView.HORIZONTAL;
                } else {
                    currentScreen = RecyclerView.VERTICAL;
                }
                updateCategoryAndScreen();
                return true;
            }
            return false;
        }
    };

    private int currentCategory;
    private int currentScreen;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_container);
        addMenuProvider(menu);
        if (savedInstanceState != null) {
            currentCategory = savedInstanceState.getInt("category");
            currentScreen = savedInstanceState.getInt("screen");
        } else {
            currentCategory = WelcomeActivity.GEOGRAPHY;
            currentScreen = RecyclerView.VERTICAL;
        }
    }

    public NavHostFragment getNavHostFragment() {
        return navHostFragment;
    }

    public int getCurrentCategory() {
        return currentCategory;
    }

    public int getCurrentScreen() {
        return currentScreen;
    }

    public void updateCategoryAndScreen() {
        while (navHostFragment.getNavController().getCurrentDestination().getId() != R.id.questionFragment) {
            navHostFragment.getNavController().popBackStack();
        }
        navHostFragment.getChildFragmentManager().getFragments().forEach(fragment -> {
            if (fragment instanceof QuestionFragment) {
                QuestionFragment questionFragment = (QuestionFragment) fragment;
                questionFragment.setCategoryAndScreen(currentCategory, currentScreen);
            }
        });
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("category", currentCategory);
        outState.putInt("screen", currentScreen);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        QuestionRepository.getInstance().saveQuestionsToDB();
    }
}